package com.rj1101.hscrm.demo.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rj1101.hscrm.demo.model.Survey;

/**
 * A data access object (DAO) providing persistence and search support for
 * Survey entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rj1101.hscrm.demo.model.Survey
 * @author MyEclipse Persistence Tools
 */

public class SurveyDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(SurveyDAO.class);
	// property constants
	public static final String SURVEY_DATE = "surveyDate";
	public static final String SURVEY_TITLE = "surveyTitle";
	public static final String SURVEY_CONTENT = "surveyContent";
	public static final String SURVEY_RATE = "surveyRate";
	public static final String SURVEY_INVESTIGATOR = "surveyInvestigator";

	protected void initDao() {
		// do nothing
	}

	public void save(Survey transientInstance) {
		log.debug("saving Survey instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Survey persistentInstance) {
		log.debug("deleting Survey instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Survey findById(java.lang.Integer id) {
		log.debug("getting Survey instance with id: " + id);
		try {
			Survey instance = (Survey) getHibernateTemplate().get(
					"com.rj1101.hscrm.demo.model.Survey", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Survey instance) {
		log.debug("finding Survey instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Survey instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Survey as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySurveyDate(Object surveyDate) {
		return findByProperty(SURVEY_DATE, surveyDate);
	}

	public List findBySurveyTitle(Object surveyTitle) {
		return findByProperty(SURVEY_TITLE, surveyTitle);
	}

	public List findBySurveyContent(Object surveyContent) {
		return findByProperty(SURVEY_CONTENT, surveyContent);
	}

	public List findBySurveyRate(Object surveyRate) {
		return findByProperty(SURVEY_RATE, surveyRate);
	}

	public List findBySurveyInvestigator(Object surveyInvestigator) {
		return findByProperty(SURVEY_INVESTIGATOR, surveyInvestigator);
	}

	public List findAll() {
		log.debug("finding all Survey instances");
		try {
			String queryString = "from Survey";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Survey merge(Survey detachedInstance) {
		log.debug("merging Survey instance");
		try {
			Survey result = (Survey) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Survey instance) {
		log.debug("attaching dirty Survey instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Survey instance) {
		log.debug("attaching clean Survey instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SurveyDAO getFromApplicationContext(ApplicationContext ctx) {
		return (SurveyDAO) ctx.getBean("SurveyDAO");
	}
}